<template>
	<view class="container" :style="[{paddingTop:CustomBar+'px'}]">
		<cu-custom bgColor="bg-colorP" :isBack="true">
			<block slot="backText"></block>
			<block slot="content">代理商入驻</block>
		</cu-custom>

		<view class="form-container">
			<view class="info-item">
				<view class='label'>姓名
					<i></i>
				</view>
				<input type='text' placeholder='请输入姓名' placeholder-class='place-hl' v-model='name'></input>
			</view>
			<view class="info-item">
				<view class='label'>手机号
					<i></i>
				</view>
				<input type='number' placeholder='请输入手机号码' placeholder-class='place-hl' v-model='phone'></input>
			</view>
			<view class="info-item">
				<view class='label'>微信号
					<i></i>
				</view>
				<input type='text' placeholder='请输入微信号' placeholder-class='place-hl' v-model='wechat'></input>
			</view>
			<view class="info-item">
				<view class='label'>身份证
					<i></i>
				</view>
				<input type='idcard' placeholder='请输入身份证号码' placeholder-class='place-hl' v-model='idCard'></input>
			</view>
		</view>
		<view class="save" @click="formSubmit">
			提交保存
		</view>
	</view>
</template>

<script>
	// 获取初始化数据
	export default {
		data() {
			return {
				CustomBar: this.CustomBar,
				name: '',
				phone: '',
				wechat: '',
				idCard: '',
			}
		},
		methods: {
			curUrlWithArgs() {
				var pages = getCurrentPages()
				var currentPage = pages[pages.length - 1]
				var url = currentPage.route
				var options = currentPage.options
				var urlWithArgs = url + '?'
				for (var key in options) {
					var value = options[key]
					urlWithArgs += key + '=' + value + '&'
				}
				urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)
				return urlWithArgs
			},
			// 提交
			formSubmit() {
				var that = this
				var params = {};
				params.agent_name = this.name;
				params.phone = this.phone;
				params.identity_card = this.idCard;
				params.wx_number = this.wechat;
				if (!params.agent_name) {
					uni.showToast({
						title: '请输入姓名',
						icon: 'none'
					})
					return false;
				}
				if (!params.phone) {
					uni.showToast({
						title: '请输入手机号',
						icon: 'none'
					})
					return false;
				} else if (!/^[1][3|4|5|6|7|8|9][0-9]{9}$/.test(params.phone)) {
					uni.showToast({
						title: '手机号格式有误',
						icon: 'none'
					})
					return false;
				}
				if (!params.wx_number) {
					uni.showToast({
						title: '请输入微信号',
						icon: 'none'
					})
					return false;
				}

				if (!params.identity_card) {
					uni.showToast({
						title: '请输入身份证',
						icon: 'none'
					})
					return false;
				} else if (!this.IdentityValid(params.identity_card)) {
					uni.showToast({
						title: '身份证格式有误',
						icon: 'none'
					})
					return false
				}


				this.$request({
					url: '/Agents/register',
					data: params,
					success: res => {
						if (res.data.status == 1) {
							uni.showModal({
								title: '提示',
								showCancel: false,
								confirmColor: '#ff0000',
								content: res.data.message,
								success: function(res) {
									if (res.confirm) {
										console.log('用户点击确定')
										that.isCommit = true
									}
								}
							})
						}
						uni.hideLoading()
					},
					fail: () => {},
					complete: () => {}
				});


			},
			// 身份证校验
			IdentityValid(code) {
				var city = {
					11: "北京",
					12: "天津",
					13: "河北",
					14: "山西",
					15: "内蒙古",
					21: "辽宁",
					22: "吉林",
					23: "黑龙江 ",
					31: "上海",
					32: "江苏",
					33: "浙江",
					34: "安徽",
					35: "福建",
					36: "江西",
					37: "山东",
					41: "河南",
					42: "湖北 ",
					43: "湖南",
					44: "广东",
					45: "广西",
					46: "海南",
					50: "重庆",
					51: "四川",
					52: "贵州",
					53: "云南",
					54: "西藏 ",
					61: "陕西",
					62: "甘肃",
					63: "青海",
					64: "宁夏",
					65: "新疆",
					71: "台湾",
					81: "香港",
					82: "澳门",
					91: "国外 "
				};
				var tip = "";

				if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)?$/i.test(code)) {
					return false;
				} else if (!city[code.substr(0, 2)]) {
					return false;
				} else {
					//18位身份证需要验证最后一位校验位
					if (code.length == 18) {
						code = code.split('');
						//∑(ai×Wi)(mod 11)
						//加权因子
						var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
						//校验位
						var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
						var sum = 0;
						var ai = 0;
						var wi = 0;
						for (var i = 0; i < 17; i++) {
							ai = code[i];
							wi = factor[i];
							sum += ai * wi;
						}
						var last = parity[sum % 11];
						if (parity[sum % 11] != code[17]) {
							return false;
						}
					}
				}
				return true;
			},
		},
		onLoad() {
			if (!uni.getStorageSync('userInfo')) {
				let curUrl = `/${this.curUrlWithArgs()}`
				curUrl = encodeURIComponent(curUrl)
				uni.redirectTo({
					url: `/pages/login?next=${curUrl}`,
				})
			}
		}
	}
</script>

<style>
	page {
		background: #fff;
	}
</style>
<style scoped>
	.form-container {
		width: 100%;
		padding: 0 24rpx;
		box-sizing: border-box;
	}
	
	.save {
		width: 702rpx;
		height: 96rpx;
		background-color: #f34930;
		border-radius: 8rpx;
		text-align: center;
		line-height: 96rpx;
		color: #fff;
		position: absolute;
		bottom: 20rpx;
		left: 50%;
		transform: translateX(-351rpx);
	}

	/*  input 公共样式 */

	.info-item {
		display: flex;
		border-bottom: 1rpx solid #edebf3;
		height: 88rpx;
		align-items: center;
		width: 100%;
		box-sizing: border-box;
		background: #fff;
	}

	.info-item .label {
		width: 140rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: justify;
		overflow: hidden;
		vertical-align: top;
		font-size: 30rpx;
		color: #4d4d4d;
		margin-right: 20rpx;
	}

	.info-item .label i {
		display: inline-block;
		width: 100%;
		height: 0;
	}

	.info-item .set {
		flex: 1;
		font-size: 32rpx;
		color: #ccc;
		display: flex;
		justify-content: flex-end;
		align-items: center;
	}

	.info-item input {
		flex: 1;
		font-size: 30rpx;
		color: #333;
		/* text-align: right; */
	}

	.info-placeholder {
		font-size: 30rpx;
		color: #ccc;
	}
</style>

<style>
	.place-hl {
		color: #999999;
	}
</style>
